﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using ArtClub.DBAL;
using System.Transactions;

namespace ArtClub.Controller
{
    public class MemberCtr
    {
        
        public Member getMember(int id)
        {
            using (DataDiagramDataContext data = new DataDiagramDataContext())
            {
                Member m = data.Members.Where(c => c.id == id).Single();
                return m;
            }
            
        }

        public string updateCronJob(DateTime date)
        {
            string message = "";
            List<Member> members = getAllMembers();
            foreach (Member member in members)
            {
                if (Convert.ToDateTime(member.lastSubscriptionDate).AddDays(27)==date)
                {
                    //simulate email sending
                    message += member.name + " has been informed\n";
                }
                if (Convert.ToDateTime(member.lastSubscriptionDate).AddMonths(1) == date)
                {
                    member.status = "Disabled";
                    message += member.name + " has been disabled\n";
                }
            }
            return message;
        }

        public List<Member> getAllMembers()
        {
            DataDiagramDataContext data = new DataDiagramDataContext();
            var table = data.GetTable<Member>();
            return table.ToList();
        }

        public double getPoints(int id)
        {
            using (DataDiagramDataContext data = new DataDiagramDataContext())
            {
                Member m = data.Members.Where(c => c.id == id).Single();
                double points = (double)m.point;
                return points;
            }
        }

        public int logIn(string name, string password)
        {
            using (DataDiagramDataContext data = new DataDiagramDataContext())
            {
                var m = data.Members.Where(c => c.userName.Equals(name) && c.password.Equals(password));
                if (m.Count() == 1)
                {
                    return 1;
                }
            }
            return -1;

        }

        public Member getMemberByUsername(string name, string password)
        {
            using (DataDiagramDataContext data = new DataDiagramDataContext())
            {
                Member m = data.Members.Where(c => c.userName.Equals(name) && c.password.Equals(password)).Single();
                return m;
            }
        }
    }
}